home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- '''Provides the default implementation for bookmarks in Orca.'''
- import pickle
- import os
- import speech
- import settings
- import orca_state
- from orca_i18n import _
-
- class Bookmarks:
- '''Represents a default bookmark handler.'''
-
- def __init__(self, script):
- self._script = script
- self._bookmarks = { }
- self._saveObservers = []
- self._loadObservers = []
- self._loadBookmarks()
- self._currentbookmarkindex = None
-
-
- def addSaveObserver(self, observer):
- self._saveObservers.append(observer)
-
-
- def addLoadObserver(self, observer):
- self._loadObservers.append(observer)
-
-
- def goToBookmark(self, inputEvent, index = None):
- ''' Go to the bookmark indexed by inputEvent.hw_code '''
- if not index:
- pass
- index = inputEvent.hw_code
-
- try:
- context = self._script.getFlatReviewContext()
- context_info = self._bookmarks[index]
- context.setCurrent(context_info['line'], context_info['zone'], context_info['word'], context_info['char'])
- self._bookmarks[index] = context_info
- except KeyError:
- self._script.systemBeep()
- return None
-
- self._script.flatReviewContext = context
- self._script.reviewCurrentItem(inputEvent)
- self._currentbookmarkindex = index
-
-
- def addBookmark(self, inputEvent):
- ''' Add an in-page accessible object bookmark for this key. '''
- context = self._script.getFlatReviewContext()
- self._bookmarks[inputEvent.hw_code] = self._contextToBookmark(context)
- utterances = [
- _('bookmark entered')]
- utterances.extend(self._script.speechGenerator.getSpeech(context.getCurrentAccessible(), False))
- speech.speakUtterances(utterances)
-
-
- def bookmarkCurrentWhereAmI(self, inputEvent):
- ''' Report "Where am I" information for this bookmark relative to the
- current pointer location.'''
-
- try:
- context = self._bookmarkToContext(self._bookmarks[inputEvent.hw_code])
- except KeyError:
- self._script.systemBeep()
- return None
-
- obj = context.getCurrentAccessible()
- cur_obj = orca_state.locusOfFocus
- if self._script.isSameObject(cur_obj, obj):
- speech.speak(_('bookmark is current object'))
- return None
- if self._script.isSameObject(cur_obj.parent, obj.parent):
- speech.speak(_('bookmark and current object have same parent'))
- return None
- bookmark_ancestors = []
- p = obj.parent
- while p:
- bookmark_ancestors.append(p)
- p = p.parent
- continue
- self._script.isSameObject(cur_obj.parent, obj.parent)
- p = cur_obj.parent
- while p:
- if bookmark_ancestors.count(p) > 0:
- speech.speak(_('shared ancestor %s') % p.role)
- return None
- p = p.parent
- continue
- bookmark_ancestors.count(p) > 0
- speech.speak(_('comparison unknown'))
-
-
- def saveBookmarks(self, inputEvent):
- ''' Save the bookmarks for this script. '''
-
- try:
- self.saveBookmarksToDisk(self._bookmarks)
- speech.speak(_('bookmarks saved'))
- except IOError:
- speech.speak(_('bookmarks could not be saved'))
-
- for o in self._saveObservers:
- o()
-
-
-
- def goToNextBookmark(self, inputEvent):
- ''' Go to the next bookmark location. If no bookmark has yet to be
- selected, the first bookmark will be used. '''
- hwkeys = self._bookmarks.keys()
- hwkeys.sort()
- if len(hwkeys) == 0:
- self._script.systemBeep()
- return None
- if len(hwkeys) == 1 or self._currentbookmarkindex is None:
- self.goToBookmark(None, index = hwkeys[0])
- return None
-
- try:
- index = hwkeys.index(self._currentbookmarkindex)
- self.goToBookmark(None, index = hwkeys[index + 1])
- except (ValueError, KeyError, IndexError):
- self._currentbookmarkindex is None
- self._currentbookmarkindex is None
- len(hwkeys) == 0
- self.goToBookmark(None, index = hwkeys[0])
- except:
- self._currentbookmarkindex is None
-
-
-
- def goToPrevBookmark(self, inputEvent):
- hwkeys = self._bookmarks.keys()
- hwkeys.sort()
- if len(hwkeys) == 0:
- self._script.systemBeep()
- return None
- if len(hwkeys) == 1 or self._currentbookmarkindex is None:
- self.goToBookmark(None, index = hwkeys[0])
- return None
-
- try:
- index = hwkeys.index(self._currentbookmarkindex)
- self.goToBookmark(None, index = hwkeys[index - 1])
- except (ValueError, KeyError, IndexError):
- self._currentbookmarkindex is None
- self._currentbookmarkindex is None
- len(hwkeys) == 0
- self.goToBookmark(None, index = hwkeys[0])
- except:
- self._currentbookmarkindex is None
-
-
-
- def _loadBookmarks(self):
- ''' Load this scripts saved bookmarks.'''
- if not self.readBookmarksFromDisk():
- pass
- self._bookmarks = { }
- for o in self._loadObservers:
- o()
-
-
-
- def readBookmarksFromDisk(self, filename = None):
- ''' Read saved bookmarks from disk. Currently an unpickled object
- that represents a bookmark '''
- if not filename:
- pass
- filename = self._script.name.split(' ')[0]
- orcaDir = settings.userPrefsDir
- orcaBookmarksDir = os.path.join(orcaDir, 'bookmarks')
-
- try:
- inputFile = open(os.path.join(orcaBookmarksDir, '%s.pkl' % filename), 'r')
- bookmarks = pickle.load(inputFile)
- inputFile.close()
- return bookmarks
- except (IOError, EOFError, OSError):
- return None
-
-
-
- def saveBookmarksToDisk(self, bookmarksObj, filename = None):
- ''' Write bookmarks to disk. bookmarksObj must be a pickleable
- object. '''
- if not filename:
- pass
- filename = self._script.name.split(' ')[0]
- orcaDir = settings.userPrefsDir
- orcaBookmarksDir = os.path.join(orcaDir, 'bookmarks')
-
- try:
- os.stat(orcaBookmarksDir)
- except OSError:
- os.mkdir(orcaBookmarksDir)
-
- output = open(os.path.join(orcaBookmarksDir, '%s.pkl' % filename), 'w', os.O_CREAT)
- pickle.dump(bookmarksObj, output)
- output.close()
-
-
- def _contextToBookmark(self, context):
- '''Converts a flat_review.Context object into a bookmark.'''
- context_info = { }
- context_info['zone'] = context.zoneIndex
- context_info['char'] = context.charIndex
- context_info['word'] = context.wordIndex
- context_info['line'] = context.lineIndex
- return context_info
-
-
- def _bookmarkToContext(self, bookmark):
- '''Converts a bookmark into a flat_review.Context object.'''
- context = self._script.getFlatReviewContext()
- context.setCurrent(bookmark['line'], bookmark['zone'], bookmark['word'], bookmark['char'])
- return context
-
-
-